#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from datetime import datetime

from sqlalchemy import UniqueConstraint, DateTime

from init_extends.init_extends import db


class NewsManage(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    n_id = db.Column(db.String(50), comment='新闻的编号')
    title = db.Column(db.String(50), comment='标题')
    abstract = db.Column(db.String(1000), nullable=True, comment='摘要')
    article_type_code = db.Column(db.String(10), comment='类型编码')
    article_type = db.Column(db.String(10), comment='类型名称')
    url = db.Column(db.String(500), comment='文章地址')
    source = db.Column(db.String(100), comment='来源')
    address = db.Column(db.String(10), nullable=True, comment='来源所属地')
    history = db.relationship('NewUpdateRecords', backref='news')
    create_time = db.Column(DateTime, default=datetime.now, comment='创建时间')

    __tablename__ = 'news_manage'
    __tablename_zh__ = '新闻管理表'
    __table_desc__ = '新闻管理表'
    __table_args__ = (
        UniqueConstraint('n_id'),
    )


class NewUpdateRecords(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    news_id = db.Column(db.Integer, db.ForeignKey('news_manage.id'))
    read_count = db.Column(db.Integer, nullable=True, comment="阅读数")
    rank_index = db.Column(db.Integer, comment="排名")
    is_top = db.Column(db.Boolean, comment="是否为置顶热搜")
    hot_score = db.Column(db.Integer, comment="热度值")
    server_update_time = db.Column(DateTime, comment='服务器刷新时间')
    create_time = db.Column(DateTime, default=datetime.now, comment='创建时间')

    __tablename__ = 'news_update_records'
    __tablename_zh__ = '新闻刷新记录表'
    __table_desc__ = '保存每次刷新时信息的信息'